<?php

namespace App\Http\Controllers\Backend\UserManage;

use App\Http\Controllers\Controller;
use App\Http\Repositories\UserManage\BusinessRepo;
use App\Http\Requests\Validators\Backend\UserManage\BusinessValidator;
use App\Traits\HelpFunctions;
use Illuminate\Http\Request;

/**
 * 服务商管理
 */
class BusinessController extends Controller
{
    use HelpFunctions;

    const VALIDATOR = BusinessValidator::class;

    public function __construct()
    {
        $this->self_repo = new BusinessRepo;
    }

    public function search(Request $request)
    {
        //参数验证
        $args = $request->only(
            'corporation_name',
            'corporation_card_id',
            'name',
            'phone',
            'landline',
            'user_name',
            'province_id',
            'city_id',
            'area_id',
            'serve_begin_time',
            'serve_end_time',
            'status',
            'readed',
            'type_ids',
            'begin_time',
            'end_time'
        );
        if (!empty($args['type_ids'])) $args['type_ids'] = explode(',', $args['type_ids']);

        $this->my_validate($args, 'searchRules');

        return $this->self_repo->search($args);
    }

    public function store(Request $request)
    {
        //参数验证
        $args = $request->only(
            'corporation_name',
            'corporation_card_id',
            'name',
            'landline',
            'province_id',
            'city_id',
            'area_id',
            'serve_begin_time',
            'serve_end_time',
            'introduction',
            'user_id',
            'resources',
            'types',
        );
        $this->my_validate($args, 'storeRules');
        if ($this->self_repo->uniqueExist($request->only('user_id'))) $this->clientError('已经存在该值');

        //添加数据
        return $this->returnResponse($this->self_repo->createOne($args));
    }

    public function delete($id)
    {
        return $this->self_repo->deleteById($id);
    }

    public function edit(Request $request, $id)
    {
        //参数验证
        $args = $request->only(
            'corporation_name',
            'corporation_card_id',
            'name',
            'landline',
            'province_id',
            'city_id',
            'area_id',
            'serve_begin_time',
            'serve_end_time',
            'introduction',
            'user_id',
            'resources',
            'types',
            'status',
            'readed',
        );
        $this->my_validate($args, 'editRules');

        //根据id修改数据
        return $this->returnResponse($this->self_repo->editById($id, $args));
    }
}
